package cn.itcast.bos.service.take_delivery.impl;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillDao;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.service.take_delivery.WayBillService;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {

	@Autowired
	private WayBillDao wayBillDao;

	// 注入索引库dao层
	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;

	@Override
	public void save(WayBill wayBill) {

		// 根据表单提交的运单id判断该运单是否在数据库存在
		WayBill persistWayBill = wayBillDao.findByWayBillNum(wayBill.getWayBillNum());

		if (persistWayBill == null || persistWayBill.getId() == null) {

			// 此时运单不存在
			wayBillDao.save(wayBill);

			// 保存到索引
			wayBillIndexRepository.save(wayBill);

		} else {
			// 此时运单存在,将提交的运单信息复制给数据库的运单，此时提交过来的waybill是没有id
			// 的，覆盖后persistWayBill也没有id，需要重新赋值

			Integer id = persistWayBill.getId();
			try {

				BeanUtils.copyProperties(persistWayBill, wayBill);// 前一个参数是目标类，后一个参数是来源类
				persistWayBill.setId(id);
				wayBillIndexRepository.save(persistWayBill);
			} catch (Exception e) {

				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}

		}

	}

	@Override
	public WayBill findByWayBillNum(String wayBillNum) {

		return wayBillDao.findByWayBillNum(wayBillNum);
	}

	// 分页查询
	@Override
	public Page<WayBill> pageQuery(WayBill wayBill, Pageable pageable) {
		// 判断分页查询中waybill是否带有条件
		if (StringUtils.isBlank(wayBill.getWayBillNum()) && StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress()) && StringUtils.isBlank(wayBill.getSendProNum())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {

			// 此时是无条件查询
			return wayBillDao.findAll(pageable);

		} else {
			// 将查询条件进行拼接
			// must,条件必须成立，相当于and,must not 条件必须不成立，相当于not,should条件可以成立，相当于or

			// 创建 布尔查询，拼接查询条件
			BoolQueryBuilder query = new BoolQueryBuilder();

			if (StringUtils.isNotBlank(wayBill.getWayBillNum())) {
				// 完全匹配查询
				QueryBuilder builder = new TermQueryBuilder("WayBillNum", wayBill.getWayBillNum());
				query.must(builder);

			}

			if (StringUtils.isNotBlank(wayBill.getSendAddress())) {
				// 此时要用到模糊查询，对输入的地址语句进行分割 ，然后与索引库的词条进行匹配
				// 例如： 输入的是"北"，是索引库查询的一部分
				QueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder("sendAddress",
						"*" + wayBill.getSendAddress() + "*");

				// 情况二： 输入的是：“北京市海淀区” 是多个词条的组合，进行分词后，每个词条匹配查询
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getSendAddress())
						.field("sendAddress").defaultOperator(Operator.AND);
				// 两种情况取其一
				BoolQueryBuilder query1 = new BoolQueryBuilder();
				query1.should(wildcardQueryBuilder);
				query1.should(queryStringQueryBuilder);
				query.must(query1);
			}

			if (StringUtils.isNotBlank(wayBill.getRecAddress())) {
				// 此时要用到模糊查询，对输入的地址语句进行分割 ，然后与索引库的词条进行匹配
				// 例如： 输入的是"北"，是索引库查询的一部分
				QueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder("recAddress",
						"*" + wayBill.getSendAddress() + "*");

				// 情况二： 输入的是：“北京市海淀区” 是多个词条的组合，进行分词后，每个词条匹配查询,and是匹配所有的词条
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getRecAddress())
						.field("recAddress").defaultOperator(Operator.AND);
				// 两种情况取其一
				BoolQueryBuilder query1 = new BoolQueryBuilder();
				query1.should(wildcardQueryBuilder);
				query1.should(queryStringQueryBuilder);
				query.must(query1);
			}

			if (StringUtils.isNotBlank(wayBill.getSendProNum())) {
				// 完全匹配查询
				QueryBuilder termQueryBuilder = new TermQueryBuilder("sendProNum", wayBill.getSendProNum());

				query.must(termQueryBuilder);

			}

			if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {

				// 完全匹配查询
				QueryBuilder termQueryBuilder = new TermQueryBuilder("signStatus", wayBill.getSignStatus());

				query.must(termQueryBuilder);

			}

			NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(query);
			
			nativeSearchQuery.setPageable(pageable);//分页效果
	
		return    wayBillIndexRepository.search(nativeSearchQuery);
		}

	}

}
